Skip to content

[Core] Replace unsafe pickle deserialization with JSON in MSAL HTTP cache#33405

Open
notyashhh wants to merge 1 commit into
devfrom
yash/icm-pickle-fix
Open

[Core] Replace unsafe pickle deserialization with JSON in MSAL HTTP cache#33405
notyashhh wants to merge 1 commit into
devfrom
yash/icm-pickle-fix

Conversation

@notyashhh
Copy link
Copy Markdown
Member

Related command
az login, az account get-access-token, and any authenticated az command that triggers MSAL token acquisition.

Description
Replaces unsafe pickle.load()/pickle.dump() with json.load()/json.dump() in BinaryCache (MSAL HTTP cache at ~/.azure/msal_http_cache.bin).

pickle.load() can execute arbitrary code during deserialization (CWE-502). If the cache file is tampered with, it results in code execution on the next az command. While default file permissions (0644) limit exploitability, pickle is unnecessary here, the cached data is simple dicts/strings (MSAL tenant discovery metadata). This was the only import pickle in the entire src/ tree.

Changes:

  • picklejson for serialization/deserialization
  • open()os.open(..., 0o600) to enforce owner-only file permissions regardless of umask
  • Updated comments to reflect JSON error types

Backward compatible, old pickle .bin files fail JSON parsing, hit the existing except Exception fallback → self.data = {} → MSAL re-fetches tenant discovery on the next command. This is the same recovery path already used for corrupted caches.

Testing Guide
Verify existing cache is gracefully discarded:
az login
az account show # Should work normally; old pickle cache silently replaced with JSON

Verify new cache is created with correct permissions:
ls -la ~/.azure/msal_http_cache.bin # Should show -rw------- (0600)

Verify cache disable still works:
az config set core.use_msal_http_cache=false
az account show # Works without cache

History Notes

[Core] Replace unsafe pickle deserialization with JSON in MSAL HTTP cache


This checklist is used to make sure that common guidelines for a pull request are followed.

@azure-client-tools-bot-prd
Copy link
Copy Markdown

azure-client-tools-bot-prd Bot commented May 20, 2026

️✔️AzureCLI-FullTest
️✔️acr
️✔️latest
️✔️3.12
️✔️3.13
️✔️acs
️✔️latest
️✔️3.12
️✔️3.13
️✔️advisor
️✔️latest
️✔️3.12
️✔️3.13
️✔️ams
️✔️latest
️✔️3.12
️✔️3.13
️✔️apim
️✔️latest
️✔️3.12
️✔️3.13
️✔️appconfig
️✔️latest
️✔️3.12
️✔️3.13
️✔️appservice
️✔️latest
️✔️3.12
️✔️3.13
️✔️aro
️✔️latest
️✔️3.12
️✔️3.13
️✔️backup
️✔️latest
️✔️3.12
️✔️3.13
️✔️batch
️✔️latest
️✔️3.12
️✔️3.13
️✔️batchai
️✔️latest
️✔️3.12
️✔️3.13
️✔️billing
️✔️latest
️✔️3.12
️✔️3.13
️✔️botservice
️✔️latest
️✔️3.12
️✔️3.13
️✔️cdn
️✔️latest
️✔️3.12
️✔️3.13
️✔️cloud
️✔️latest
️✔️3.12
️✔️3.13
️✔️cognitiveservices
️✔️latest
️✔️3.12
️✔️3.13
️✔️compute_recommender
️✔️latest
️✔️3.12
️✔️3.13
️✔️computefleet
️✔️latest
️✔️3.12
️✔️3.13
️✔️config
️✔️latest
️✔️3.12
️✔️3.13
️✔️configure
️✔️latest
️✔️3.12
️✔️3.13
️✔️consumption
️✔️latest
️✔️3.12
️✔️3.13
️✔️container
️✔️latest
️✔️3.12
️✔️3.13
️✔️containerapp
️✔️latest
️✔️3.12
️✔️3.13
️✔️core
️✔️latest
️✔️3.12
️✔️3.13
️✔️cosmosdb
️✔️latest
️✔️3.12
️✔️3.13
️✔️databoxedge
️✔️latest
️✔️3.12
️✔️3.13
️✔️dls
️✔️latest
️✔️3.12
️✔️3.13
️✔️dms
️✔️latest
️✔️3.12
️✔️3.13
️✔️eventgrid
️✔️latest
️✔️3.12
️✔️3.13
️✔️eventhubs
️✔️latest
️✔️3.12
️✔️3.13
️✔️feedback
️✔️latest
️✔️3.12
️✔️3.13
️✔️find
️✔️latest
️✔️3.12
️✔️3.13
️✔️hdinsight
️✔️latest
️✔️3.12
️✔️3.13
️✔️identity
️✔️latest
️✔️3.12
️✔️3.13
️✔️iot
️✔️latest
️✔️3.12
️✔️3.13
️✔️keyvault
️✔️latest
️✔️3.12
️✔️3.13
️✔️lab
️✔️latest
️✔️3.12
️✔️3.13
️✔️managedservices
️✔️latest
️✔️3.12
️✔️3.13
️✔️maps
️✔️latest
️✔️3.12
️✔️3.13
️✔️marketplaceordering
️✔️latest
️✔️3.12
️✔️3.13
️✔️monitor
️✔️latest
️✔️3.12
️✔️3.13
️✔️mysql
️✔️latest
️✔️3.12
️✔️3.13
️✔️netappfiles
️✔️latest
️✔️3.12
️✔️3.13
️✔️network
️✔️latest
️✔️3.12
️✔️3.13
️✔️policyinsights
️✔️latest
️✔️3.12
️✔️3.13
️✔️postgresql
️✔️latest
️✔️3.12
️✔️3.13
️✔️privatedns
️✔️latest
️✔️3.12
️✔️3.13
️✔️profile
️✔️latest
️✔️3.12
️✔️3.13
️✔️rdbms
️✔️latest
️✔️3.12
️✔️3.13
️✔️redis
️✔️latest
️✔️3.12
️✔️3.13
️✔️relay
️✔️latest
️✔️3.12
️✔️3.13
️✔️resource
️✔️latest
️✔️3.12
️✔️3.13
️✔️role
️✔️latest
️✔️3.12
️✔️3.13
️✔️search
️✔️latest
️✔️3.12
️✔️3.13
️✔️security
️✔️latest
️✔️3.12
️✔️3.13
️✔️servicebus
️✔️latest
️✔️3.12
️✔️3.13
️✔️serviceconnector
️✔️latest
️✔️3.12
️✔️3.13
️✔️servicefabric
️✔️latest
️✔️3.12
️✔️3.13
️✔️signalr
️✔️latest
️✔️3.12
️✔️3.13
️✔️sql
️✔️latest
️✔️3.12
️✔️3.13
️✔️sqlvm
️✔️latest
️✔️3.12
️✔️3.13
️✔️storage
️✔️latest
️✔️3.12
️✔️3.13
️✔️synapse
️✔️latest
️✔️3.12
️✔️3.13
️✔️telemetry
️✔️latest
️✔️3.12
️✔️3.13
️✔️util
️✔️latest
️✔️3.12
️✔️3.13
️✔️vm
️✔️latest
️✔️3.12
️✔️3.13

@azure-client-tools-bot-prd
Copy link
Copy Markdown

Hi @notyashhh,
Since the current milestone time is less than 7 days, this pr will be reviewed in the next milestone.

@azure-client-tools-bot-prd
Copy link
Copy Markdown

azure-client-tools-bot-prd Bot commented May 20, 2026

️✔️AzureCLI-BreakingChangeTest
️✔️Non Breaking Changes

@notyashhh notyashhh marked this pull request as ready for review May 20, 2026 06:17
Copilot AI review requested due to automatic review settings May 20, 2026 06:17
@microsoft-github-policy-service microsoft-github-policy-service Bot added the Auto-Assign Auto assign by bot label May 20, 2026
@microsoft-github-policy-service microsoft-github-policy-service Bot added ARM az resource/group/lock/tag/deployment/policy/managementapp/account management-group act-identity-squad labels May 20, 2026
@yonzhan
Copy link
Copy Markdown
Collaborator

yonzhan commented May 20, 2026

Thank you for your contribution! We will review the pull request and get back to you soon.

@github-actions
Copy link
Copy Markdown

The git hooks are available for azure-cli and azure-cli-extensions repos. They could help you run required checks before creating the PR.

Please sync the latest code with latest dev branch (for azure-cli) or main branch (for azure-cli-extensions).
After that please run the following commands to enable git hooks:

pip install azdev --upgrade
azdev setup -c <your azure-cli repo path> -r <your azure-cli-extensions repo path>

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens Azure CLI’s MSAL HTTP cache handling (used during az login and authenticated token acquisition) by removing unsafe pickle deserialization and writing the cache with stricter file permissions.

Changes:

  • Replace pickle.load()/pickle.dump() with json.load()/json.dump() for the MSAL HTTP cache file.
  • Switch cache writes to os.open(..., 0o600) + os.fdopen(...) to create new cache files with owner-only permissions.
  • Update inline comments describing expected deserialization errors.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

with open(self.filename, 'rb') as f:
return pickle.load(f)
with open(self.filename, 'r', encoding='utf-8') as f:
return json.load(f)
Comment on lines +48 to +49
# - json.JSONDecodeError is caused by corrupted or legacy pickle cache file.
# - ValueError/KeyError from malformed JSON content.
# raise EOFError. This can be simulated by adding time.sleep(30) here.
# So during loading, EOFError is ignored.
pickle.dump(self.data, f)
fd = os.open(self.filename, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
@yonzhan yonzhan assigned isra-fel and unassigned jiasli and zhoxing-ms May 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Account az login/account act-identity-squad act-platform-engineering-squad ARM az resource/group/lock/tag/deployment/policy/managementapp/account management-group Auto-Assign Auto assign by bot Core CLI core infrastructure

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants